import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";
import { ResponseModel } from "../../src/model/ResponseModel";

export class User_Meilis_HistoryDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;

    public static async add_user_meilis_history(param: object) {
        let userid: number = param["userid"]
        let meilis: number = param["meilis"]
        let left_meilis: number = param["left_meilis"]
        let reseason: string = param["reseason"]
        let detail: string = param["detail"]
        let time: string = param["time"]
        let sql: string = `INSERT INTO t_user_meilis_history(userid, meilis,left_meilis, reseason, time, detail) VALUES(?,?,?,?,?,?) ;`
        let sqlParams = [userid, meilis, left_meilis, reseason, time, detail];
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async clear(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_user_meilis_history where time < ?"
        let sqlParams = [DateUtil.formatDateStr(delete_time)];
        client.queryPromise(sql, sqlParams);
    }

    public static async get_meilis_detail(pagenum: number, pagesize: number, userid: number, startTime: string, endTime: string) {
        let respModel: ResponseModel = new ResponseModel()
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let start: number = (pagenum - 1) * pagesize
        let sql: string = "select * from t_user_meilis_history WHERE time >= ? and time < ?  ";
        if (userid > 0) {
            sql += " and userid=" + userid
        }
        sql += " order by time desc "
        sql += " limit ?,?"
        let sqlParams = [startTime, endTime, start, pagesize];
        let results = await client.queryPromise(sql, sqlParams);
        // Logger.log("results===", results)

        sql = "select count(*) as count from t_user_meilis_history  WHERE time >= ? and time < ?"
        if (userid > 0) {
            sql += " and userid=" + userid
        }
        sqlParams = [startTime, endTime];
        let resultObj: object = {
            "data": results,
        }
        results = await client.queryPromise(sql, sqlParams);
        let count: number = results[0]["count"]
        resultObj["count"] = count
        respModel.data = resultObj
        return respModel
    }

}